package com.jn.erp.web.api.course.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.utils.DateUtils;
import com.jn.erp.web.api.course.entity.TCourseOrder;
import com.jn.erp.web.api.course.entity.TCourseOrderConfig;
import com.jn.erp.web.api.course.entity.dto.TCourseOrderConfigDto;
import com.jn.erp.web.api.course.entity.vo.TCourseOrderConfigVo;
import com.jn.erp.web.api.course.mapper.TCourseOrderConfigMapper;

import com.jn.erp.web.api.course.mapper.TCourseOrderMapper;
import com.jn.erp.web.api.course.service.TCourseOrderConfigService;
import com.jn.erp.web.api.course.task.TCourseOrderTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wyp
 * @since 2024-09-03
 */
@Service
public class TCourseOrderConfigServiceImpl extends ServiceImpl<TCourseOrderConfigMapper, TCourseOrderConfig> implements TCourseOrderConfigService {

    @Autowired
    private TCourseOrderTask tCourseOrderTask;

    @Autowired
    private TCourseOrderMapper tCourseOrderMapper;
    @Override
    public Boolean saveCourseOrder(TCourseOrderConfigVo tCourseOrderConfigVo) {
        if(1==tCourseOrderConfigVo.getCirculateType()){
            tCourseOrderConfigVo.setWeekNum(null);
        }
        this.baseMapper.insert(tCourseOrderConfigVo);
        //开线程池，线程数量为要遍历的对象的长度
        ExecutorService executor = Executors.newFixedThreadPool(1);
        CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
            Date ss = new Date();
            tCourseOrderTask.createCourseOrder();
            Date ee = new Date();
            System.out.printf("创建排课记录执行时间：%d 毫秒." , (ee.getTime() - ss.getTime()));
        },executor);
        return true;
    }

    @Override
    public Boolean updateCourseOrder(TCourseOrderConfigVo tCourseOrderConfigVo) {
        this.baseMapper.updateById(tCourseOrderConfigVo);
        TCourseOrder tCourseOrder = new TCourseOrder();
//        tCourseOrder.setCofId(tCourseOrderConfigVo.getId());
        tCourseOrder.setIsDelete("1");
        //需添加当天大于等于开课时间
        this.tCourseOrderMapper.update(tCourseOrder,new QueryWrapper<TCourseOrder>().eq("cof_id",tCourseOrderConfigVo.getId())
                .ge("class_date",LocalDateTime.now()).eq("state","1"));
        //开线程池，线程数量为要遍历的对象的长度
        ExecutorService executor = Executors.newFixedThreadPool(1);
        CompletableFuture completableFuture = CompletableFuture.runAsync(() -> {
            Date ss = new Date();
            tCourseOrderTask.createCourseOrder();
            Date ee = new Date();
            System.out.printf("创建排课记录执行时间：%d 毫秒." , (ee.getTime() - ss.getTime()));
        },executor);
        return true;
    }

    @Override
    public List<TCourseOrderConfigDto> courseOrderConfigList(TCourseOrderConfigVo tCourseOrderConfigVo) {
        List<TCourseOrderConfigDto> tCourseOrderConfigDtos = this.baseMapper.courseOrderConfigList(tCourseOrderConfigVo);
        return tCourseOrderConfigDtos;
    }

    @Override
    public TCourseOrderConfig getCourseOrderConfig(TCourseOrderConfigVo tCourseOrderConfigVo) {
        TCourseOrderConfig tCourseOrderConfig = this.baseMapper.selectById(tCourseOrderConfigVo.getId());
        if(!StringUtils.isEmpty(tCourseOrderConfig.getAssistant())) {
            String[] split = tCourseOrderConfig.getAssistant().split(",");
            if (split.length > 0) {
                List<Long> collect = Arrays.stream(split).map(u -> {
                    return Long.parseLong(u);
                }).collect(Collectors.toList());
                tCourseOrderConfig.setAssistantArr(collect);
            }
        }
        return tCourseOrderConfig;
    }


}
